{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4b915287",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Parameter Management\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "41cbf7e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:20.807089Z",
     "iopub.status.busy": "2023-08-18T19:27:20.806426Z",
     "iopub.status.idle": "2023-08-18T19:27:22.457089Z",
     "shell.execute_reply": "2023-08-18T19:27:22.456154Z"
    },
    "origin_pos": 3,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bb0f644",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "We start by focusing on an MLP with one hidden layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9aa0461f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:22.461279Z",
     "iopub.status.busy": "2023-08-18T19:27:22.460607Z",
     "iopub.status.idle": "2023-08-18T19:27:22.494399Z",
     "shell.execute_reply": "2023-08-18T19:27:22.493545Z"
    },
    "origin_pos": 8,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 1])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = nn.Sequential(nn.LazyLinear(8),\n",
    "                    nn.ReLU(),\n",
    "                    nn.LazyLinear(1))\n",
    "\n",
    "X = torch.rand(size=(2, 4))\n",
    "net(X).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfd25db6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Parameter Access"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3c6fdb60",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:22.497996Z",
     "iopub.status.busy": "2023-08-18T19:27:22.497442Z",
     "iopub.status.idle": "2023-08-18T19:27:22.504291Z",
     "shell.execute_reply": "2023-08-18T19:27:22.503521Z"
    },
    "origin_pos": 16,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('weight',\n",
       "              tensor([[-0.1649,  0.0605,  0.1694, -0.2524,  0.3526, -0.3414, -0.2322,  0.0822]])),\n",
       "             ('bias', tensor([0.0709]))])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[2].state_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f0c0f12",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Targeted Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ba2da7b4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:22.507849Z",
     "iopub.status.busy": "2023-08-18T19:27:22.507181Z",
     "iopub.status.idle": "2023-08-18T19:27:22.513236Z",
     "shell.execute_reply": "2023-08-18T19:27:22.512406Z"
    },
    "origin_pos": 21,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.nn.parameter.Parameter, tensor([0.0709]))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(net[2].bias), net[2].bias.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c5f0ae9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:22.516723Z",
     "iopub.status.busy": "2023-08-18T19:27:22.516170Z",
     "iopub.status.idle": "2023-08-18T19:27:22.521606Z",
     "shell.execute_reply": "2023-08-18T19:27:22.520790Z"
    },
    "origin_pos": 27,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[2].weight.grad == None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3742539f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "All Parameters at Once"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dab1b4b5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:22.525019Z",
     "iopub.status.busy": "2023-08-18T19:27:22.524380Z",
     "iopub.status.idle": "2023-08-18T19:27:22.530002Z",
     "shell.execute_reply": "2023-08-18T19:27:22.529195Z"
    },
    "origin_pos": 30,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('0.weight', torch.Size([8, 4])),\n",
       " ('0.bias', torch.Size([8])),\n",
       " ('2.weight', torch.Size([1, 8])),\n",
       " ('2.bias', torch.Size([1]))]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[(name, param.shape) for name, param in net.named_parameters()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd5bd1a5",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Tied Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5b706636",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:27:22.533421Z",
     "iopub.status.busy": "2023-08-18T19:27:22.532786Z",
     "iopub.status.idle": "2023-08-18T19:27:22.541856Z",
     "shell.execute_reply": "2023-08-18T19:27:22.541011Z"
    },
    "origin_pos": 35,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([True, True, True, True, True, True, True, True])\n",
      "tensor([True, True, True, True, True, True, True, True])\n"
     ]
    }
   ],
   "source": [
    "shared = nn.LazyLinear(8)\n",
    "net = nn.Sequential(nn.LazyLinear(8), nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    nn.LazyLinear(1))\n",
    "\n",
    "net(X)\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])\n",
    "net[2].weight.data[0, 0] = 100\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "required_libs": [],
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}